package com.dci.service.impl.project;

import com.dci.dao.pms.management.ProjectManageInfoMapper;
import com.dci.dao.pms.project.OutboundInfoMapper;
import com.dci.dao.pms.project.ProjectInfoMapper;
import com.dci.model.management.ProjectManageInfo;
import com.dci.model.project.OutboundInfo;
import com.dci.model.project.ProjectInfo;
import com.dci.result.JsonData;
import com.dci.service.project.OutboundInfoService;
import com.dci.util.UUIDBuild;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.service.impl.project
 * @Description: 出库管理实现类
 * @author: 张星亮
 * @date: 2019/2/25
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OutboundInfoServiceImpl implements OutboundInfoService {

    @Resource
    private OutboundInfoMapper outboundInfoMapper;

    @Resource
    private ProjectInfoMapper projectInfoMapper;

    @Resource
    private ProjectManageInfoMapper projectManageInfoMapper;


    /**
     * 出库申请
     *
     * @param record
     * @return
     */
    @Override
    public JsonData insert(OutboundInfo record, String fileNames) {
        record.setId(UUIDBuild.getUUID());
        // 默认1, 申请出库
        record.setStatus(1);
        outboundInfoMapper.insert(record);

        // 更新项目出库状态
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setId(record.getProjectId());
        projectInfo.setOutboundStatus(1);
        projectInfo.setOutboundFile(fileNames);
        projectInfoMapper.updateByPrimaryKeySelective(projectInfo);

        return JsonData.success(record, 906);
    }

    @Override
    public JsonData getOtuBoundInfo(OutboundInfo record) {
        record = outboundInfoMapper.selectByPrimaryKey(record);
        return JsonData.success(record, 906);
    }

    @Override
    public JsonData updateAuditInfo(OutboundInfo record) {
        outboundInfoMapper.updateByPrimaryKeySelective(record);
        record = outboundInfoMapper.selectByPrimaryKey(record);

        // 更新项目出库状态
        updateProjectStatusByOut(record.getProjectId(), 2, record.getOutboundReason());
        return JsonData.success(record, 906);
    }

    @Override
    public JsonData selectByProjectId(OutboundInfo record, List<ProjectInfo> listP) {
        String file_url = listP.get(0).getFilePath();
        String project_file = listP.get(0).getProjectFile();
        String evaluate_file = listP.get(0).getEvaluateFile();
        String leader_file = listP.get(0).getLeaderFile();
        String outbound_file = listP.get(0).getOutboundFile();
        String groupOpinion = listP.get(0).getGroupOpinion();
        String leaderOpinion = listP.get(0).getLeaderOpinion();
        List<OutboundInfo> list = outboundInfoMapper.selectByProjectId(record);
        for (int a = 0; a < list.size(); a++) {
            list.get(a).setFile_url(file_url);
            list.get(a).setProject_file(project_file);
            list.get(a).setEvaluate_file(evaluate_file);
            list.get(a).setLeader_file(leader_file);
            list.get(a).setOutbound_file(outbound_file);
            list.get(a).setGroupOpinion(groupOpinion);
            list.get(0).setLeaderOpinion(leaderOpinion);
        }
        return JsonData.success(list, 906);
    }

    @Override
    public JsonData auditMove(OutboundInfo record) {
        String projectId = record.getId();
        String reason = record.getReason();
        outboundInfoMapper.updateByPrimaryKeySelective(record);
        record = outboundInfoMapper.selectByPrimaryKey(record);
        if (record == null) {
            updateProjectStatusByApproval(projectId, 3, reason);
        } else {
            // 更新项目出库状态
            updateProjectStatus(record.getProjectId(), 3, reason);
        }

        return JsonData.success(record, 906);
    }


    /**
     * 更新项目 移交状态
     *
     * @param id
     * @param status
     * @return
     */
    public int updateProjectStatus(String id, int status, String reason) {
        // 更新项目出库状态
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setId(id);
        ProjectInfo projectInfo1 = projectInfoMapper.selectByPrimaryKey(projectInfo);
        if (reason.equals("已纳入年度投资计划的实施项目")) {
            projectInfo.setOutboundStatus(status);
            ProjectManageInfo projectManageInfo = new ProjectManageInfo();
            projectManageInfo.setId(projectInfo1.getId());
            projectManageInfo.setName(projectInfo1.getName());
            projectManageInfo.setType(1);
            projectManageInfo.setHead(projectInfo1.getHead());
            projectManageInfo.setHeadPhone(projectInfo1.getHeadPhone());
            projectManageInfo.setContact(projectInfo1.getContact());
            projectManageInfo.setContactPhone(projectInfo1.getContactPhone());
            projectManageInfo.setConstructionUnit(projectInfo1.getConstructionUnit());
            projectManageInfo.setImplementUnit(projectInfo1.getImplementUnit());
            projectManageInfo.setImplementLimit(projectInfo1.getImplementLimit());
            projectManageInfo.setMoneyInfo(projectInfo1.getMoneyInfo());
            projectManageInfo.setDescription(projectInfo1.getDescription());
            projectManageInfo.setDeclareTime(projectInfo1.getDeclareTime());
            projectManageInfoMapper.insert(projectManageInfo);
        } else {
            projectInfo.setOutboundStatus(status);
            projectInfo.setApprovalType(3);
        }
        // 1 出库审核通过

        projectInfoMapper.updateByPrimaryKeySelective(projectInfo);

        return projectInfoMapper.updateByPrimaryKeySelective(projectInfo);
    }

    /**
     * 更新项目 审批未通过归档
     *
     * @param id
     * @param status
     * @return
     */
    public int updateProjectStatusByApproval(String id, int status, String reason) {
        // 更新项目出库状态
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setId(id);
        projectInfo.setOutboundStatus(status);
        projectInfo.setApprovalStatus(1);
        projectInfo.setApprovalType(3);

        projectInfoMapper.updateByPrimaryKeySelective(projectInfo);

        return projectInfoMapper.updateByPrimaryKeySelective(projectInfo);
    }

    /**
     * 更新项目 出库状态
     *
     * @param id
     * @param status
     * @return
     */
    public int updateProjectStatusByOut(String id, int status, int outboundReason) {
        // 更新项目出库状态
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setId(id);
        projectInfo.setOutboundStatus(status);
        // 1 出库审核通过
        if (outboundReason != 1 && outboundReason != 5) {
            projectInfo.setOutboundStatus(2);
        }
        projectInfoMapper.updateByPrimaryKeySelective(projectInfo);

        return projectInfoMapper.updateByPrimaryKeySelective(projectInfo);
    }

}
